function initCart(){
    addToCart();
    removeCartItem();
    updateCartItem();
}

function calculateCart(){
    $.ajax({
        url:$('.cart-total-item').attr('data-href'),
        method: 'GET',
        success: function(data){
            $('.cart-total-item').html(data);
        }
    });
}

function addToCart(){
    $(document.body).on('click', '#btn-add-to-cart', function(){
        var form = $(this).closest('form');
        var element = $(this);
        $.ajax({
            url: element.attr('data-href'),
            data: form.serialize(),
            type: "POST",
            cache: false,
            success: function(data){
                if(data==200){
                    calculateCart();
                    if (flashMessage !== undefined) {
                        var div = $('div#flg_message');
                        flashMessage(div, '<i class="fa fa-check-circle"></i>&nbsp;Đã thêm vào giỏ hàng');
                    }                    
                    if(typeof updateCartContent !== typeof undefined){
                        updateCartContent();
                    }
                }
            }
        });
    });

    $(document.body).on('click', '.btn-add-to-cart', function(e){
        e.preventDefault();
        var element = $(this);
        $.ajax({
            url: element.attr('data-href'),
            data: {id: element.attr('data-id')},
            type: "POST",
            cache: false,
            success: function(data){
                if(data==200){
                    calculateCart();
                    if (flashMessage !== undefined) {
                        var div = element.closest('div.caption').find('div.flg_message');
                        flashMessage(div, '<i class="fa fa-check-circle"></i>&nbsp;Đã thêm vào giỏ hàng');
                    }
                    if(makeToast !== undefined){
                        var title = element.parents('.single-products').find('.product-title').html();
                        makeToast('Đã thêm <strong>' + title + '</strong> vào giỏ hàng', 1);
                    }
                    if(typeof updateCartContent !== typeof undefined){
                        updateCartContent();
                    }
                }
            }
        });
        return false;
    });
}

function removeCartItem(){
    $(document.body).on('click', '.btn-remove-cart-item', function(e){
        e.preventDefault();
        var tr = $(this).closest('tr');
        $.ajax({
            url: $(this).closest('table').attr('action-remove'),
            data: 'item='+$(this).attr('item'),
            type: "POST",
            cache: false,
            success: function(data){
                if(data==200){
                    tr.remove();
                    updateCartResult(tr);
                    calculateCart();
                }
            }
        });
        return false;
    });
}

function updateCartItem(){
    $(document.body).on('click', '.btn-update-cart-item', function(e){
        e.preventDefault();
        var tr = $(this).closest('tr');
        var item = $(this).attr('item');
        var quantity = tr.find('td.col-cart-qty input.item-quantity');
        if(isNaN(quantity.val())) {
            alert('Số lượng không hợp lệ');
            quantity.focus();
        } else {
            $.ajax({
                url: $(this).closest('table').attr('action-update'),
                data: {
                    item: item,
                    quantity: quantity.val()
                },
                type: "POST",
                cache: false,
                success: function(data){
                    if(data > 0){
                        tr.find('.col-cart-subtotal span').html(formatCurrency(data));
                        updateCartResult(tr);
                        calculateCart();
                    }
                }
            });
        }
        return false;
    });
}

function updateCartResult(tr){
    $.ajax({
        url: tr.closest('table').attr('action-result'),
        type: "POST",
        cache: false,
        success: function(data){
            if(data.total_items!=undefined){
                $('#total-items').html(data.total_items);
                $('#total-price').html(formatCurrency(data.total_price));
            }
        }
    });
}